import 'package:flutter/material.dart';
import 'package:fsharing/core/models/logistics.dart';
import 'package:fsharing/core/models/order_detail.dart';
import 'package:fsharing/core/viewmodels/pages/mine/order/logistics_model.dart';
import 'package:fsharing/core/viewmodels/view_model.dart';
import 'package:fsharing/main.dart';
import 'package:fsharing/ui/widgets/background_container.dart';
import 'package:fsharing/ui/widgets/round_back_button.dart';
import 'package:provider/provider.dart';

class LogisticsPage extends StatelessWidget {
  final Invoice invoice;

  LogisticsPage({
    Key key,
    @required this.invoice,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => LogisticsModel(Provider.of(context, listen: false), invoice),
      child: BackgroundContainer(
        child: Scaffold(
          appBar: AppBar(
            leading: RoundBackButton(),
            title: Text('物流信息'),
          ),
          body: Consumer<LogisticsModel>(
            builder: (context, model, child) {
              switch (model.viewState) {
                case ViewState.busy:
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                case ViewState.error:
                  return Center(
                    child: Text(model.errorMessage),
                  );
                case ViewState.success:
                  return _BodyWidget();
                default:
                  return Container();
              }
            },
            child: Container(),
          ),
        ),
      ),
    );
  }
}

class _BodyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    LogisticsModel model = Provider.of(context, listen: false);
    Logistics logistics = model.logistics;
    return ListView.builder(
      itemBuilder: (context, index) {
        Trace trace = logistics.traces[index];
        return Theme(
          data: lightTheme(),
          child: Material(
            child: ListTile(
              title: Text(trace.acceptStation),
              subtitle: Text(trace.acceptTime),
            ),
          ),
        );
      },
      itemCount: logistics.traces.length,
    );
  }
}
